'use strict';

angular.module('fbdApp.templateModule')

.controller('templateCtrl', function($scope, $compile, shareData,
  modalService, $sce, fbdService, $filter) {
  $scope.testType = 'file';

  $scope.testing = {
    mode: 'signs',
    name: 'testing'
  };
  // $scope.testing = 'file';
  console.log($scope.testType);
  $scope.newTemp = true;
  $scope.upload = false;
  // $scope.mainRule = false;
  $scope.addSubrule = false;
  // $scope.isMain = true;
  $scope.isMainRule; //是否是主规则
  $scope.isMainTemp = true; //是否是主模板



  // }

  $scope.$on('isUploaded', function(event, data) {
    $scope.isUploaded = data;
    // console.log('isUploaded', $scope.isUploaded);
    // console.log($scope.isUploaded, 'isUploaded');
  });

  // $scope.$on('fileName', function(event, data) {
  //   $scope.fileName = data;
  //   // console.log('fileName', $scope.fileName);
  //   // console.log($scope.fileName, 'finally');
  // });

  // $scope.$on('articleList', function(event, data) {
  //   $scope.articleList = data;
  //   // console.log(data, 'onarti');
  // });


  $scope.newTempToNext = function() {
    $scope.newTemp = false;
    $scope.isMainRule = true;
    // 如果是主模板，才需要上传文件
    if ($scope.isMainTemp) {
      $scope.upload = true;
    }
    // 如果不是主模板，直接添加主规则
    else {
      // $scope.upload = false;
      $scope.addNewRules();
    }
  };

  $scope.toNewTemp = function() {
    $scope.newTemp = true;
    $scope.upload = false;
  };

  $scope.addNewRules = function() {
    $scope.newTemp = false;
    $scope.upload = false;
    $scope.addSubrule = true;
    // 动态新增规则
    var elem;
    // 主规则
    if ($scope.isMainRule && $scope.isMainTemp) {
      elem = $compile('<new-rule test-type="file"></new-rule>')(
        $scope);
      $scope.isMainRule = false;
    }
    // 子规则
    else {
      elem = $compile('<new-rule test-type="string"></new-rule>')(
        $scope);
      // $scope.addSubrule = true;
    }
    angular.element(document.querySelector('.triggerNewRule')).after(
      elem);
    // $scope.$broadcast('FileName', $scope.fileName);
  };

  $scope.items = ['item1', 'item2', 'item3'];

  $scope.open = function() {
    var modalConfig = {
      controller: 'rulesCtrl'
    };

    var modalInfo = {
      closeButtonText: '取消',
      actionButtonText: '确定',
      headerText: '添加规则',
      bodyText: '<new-rule test-type="file"></new-rule>'
    };

    modalService.showModal({}, modalInfo).result.then(function(
      result) {
      $scope.testResult = result;
    }, function(reason) {
      $scope.testResult = reason;
    });
  };
  // $scope.open = function(size) {
  //
  //   var modalInstance = $uibModal.open({
  //     animation: true,
  //     template: "<new-rule test-type='file'></new-rule>",
  //     controller: 'modalInstanceController',
  //     size: size,
  //     resolve: {
  //       items: function() {
  //         return $scope.items;
  //       }
  //     }
  //   });
  //
  //   modalInstance.result.then(function(selectedItem) {
  //     $scope.selected = selectedItem;
  //   }, function() {
  //     console.log("modal dismissed at: " + new Date());
  //   });
  //
  // };
  // $scope.addRule = function() {
  //
  //   // 添加子规则
  //   var elem = $compile('<new-rule test-type="string"></new-rule>')(
  //     $scope);
  //   // $scope.$digest();
  //   angular.element('#triggerNewRule').before(elem);
  //
  //   $scope.$broadcast('articles', $scope.articleList);
  //   // console.log($scope.articleList, 'addRule');
  //   // 广播文章列表
  // };

  // if (shareData.rules.length != 0) {
  $scope.selectedRules = shareData.rules;
  // console.log($scope.selectedRules);
  // $scope.fileName = shareData.readFileName;
  // $scope.testObj = {
  //   type: 'file',
  //   content: shareData.fileName
  // };
  $scope.testTemp = function() {
    // $scope.fileName = shareData.fileName;
    $scope.testObj = {
      type: 'file',
      content: shareData.fileName
    };
    // $scope.fileName = shareData.realFileName;
    $scope.postData = $filter('templateFilter')($scope.selectedRules,
      $scope.testObj);
    // console.log($scope.postData);
    var promise = fbdService.test(
      'http://localhost:3000/api/templates/detect',
      $scope.postData);
    promise.then(function(res) {

      console.log(res);


    }, function(err) {
      // $scope.testRuleResult.info = err + '，请重新发送请求';
      // $scope.testRuleResult.data = [];
      console.log(err);
    });
  };

  // var rules = [{
  //     mode: 'signs',
  //     beginText: '<<<',
  //     type: 1,
  //     expression: '123',
  //     name: 'rule1'
  //   }, {
  //     mode: 'signs',
  //     beginText: '<<<',
  //     endText: 'a',
  //     type: 2,
  //     expression: '12312',
  //     name: 'rule2'
  //   }, {
  //     mode: 'signs',
  //     beginText: '<<<',
  //     endText: 'a',
  //     type: 2,
  //     expression: '12312',
  //     name: 'rule3'
  //   },
  //   [{
  //     mode: 'signs',
  //     beginText: '<<<',
  //     type: 1,
  //     expression: '123',
  //     name: 'sub1'
  //   }, {
  //     mode: 'signs',
  //     beginText: '<<<',
  //     type: 1,
  //     expression: '123',
  //     name: 'sub2'
  //   }]
  // ];

  // var result = {
  //   main: {
  //     rule1: [{
  //       location: [15, 23],
  //       rule2: [{
  //         location: [0, 2],
  //         text: 'ss'
  //       }, {
  //         location: [0, 2],
  //         text: 's1'
  //       }],
  //       rule3: [{
  //         location: [0, 2],
  //         text: 'ss'
  //       }],
  //       valid: true,
  //       text: 'abcd'
  //     }, {
  //       location: [15, 23],
  //       rule2: [{
  //         location: [0, 2],
  //         text: 'ssaa'
  //       }],
  //       rule3: [{
  //         location: [0, 2],
  //         text: 'ssaaa'
  //       }],
  //       valid: true,
  //       text: 'abcd'
  //     }],
  //     valid: true,
  //     text: 'abcdsdfsd'
  //   },
  //   others: {}
  // };

  // $scope.show = $filter('showTempFilter')(rules, result);

  $scope.saveTemp = function() {
    $scope.addData = {
      name: $scope.tempName,
      content: $scope.postData.template
    };
    var promise = fbdService.save(
      'http://localhost:3000/api/templates/add',
      $scope.addData);
    promise.then(function(res) {
      $scope.show = $filter('showTempFilter')($scope.selectedRules,
        res);
      // console.log(res);


    }, function(err) {
      // $scope.testRuleResult.info = err + '，请重新发送请求';
      // $scope.testRuleResult.data = [];
      console.log(err);
    });
  };

});
// .controller('modalInstanceController', function($scope, $uibModalInstance,
//   items) {
//   $scope.items = items;
//   $scope.selected = {
//     item: $scope.items[0]
//   };
//   $scope.ok = function() {
//     $uibModalInstance.close($scope.selected.item);
//   };
//
//   $scope.cancel = function() {
//     $uibModalInstance.dismiss('cancel');
//   };
// });
